<template>
  <div class="goods">
    <!-- 头部搜索 -->
    <div class="header">
      <el-input
        placeholder="请输入内容"
        v-model="input"
        @change="searchIpu"
        clearable
      >
      </el-input>
      <el-button type="primary">查询</el-button>
      <el-button type="primary">
        <router-link to="/add-goods" style="color: #fff">添加</router-link>
      </el-button>
      <el-button type="primary" @click="addGoods"> 弹窗添加 </el-button>
    </div>
    <!-- 表格动态数据展示 -->
    <div class="wapper">
      <el-table
        :data="tableData"
        style="width: 100%"
        ref="multipleTable"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="商品ID" width="120">
        </el-table-column>
        <el-table-column prop="title" label="商品名称" width="120">
        </el-table-column>
        <el-table-column prop="price" label="商品价格" width="120">
        </el-table-column>
        <el-table-column prop="num" label="商品数量" width="120">
        </el-table-column>
        <el-table-column
          prop="category"
          label="规格类目"
          width="120"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="image" label="商品图片" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="sellPoint"
          label="商品卖点"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="descs"
          label="商品描述"
          width="180"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 10px">
        <el-button type="primary" size="small" @click="selectAll"
          >全选</el-button
        >
        <el-button
          type="primary"
          size="small"
          @click="toggleSelection(tableData)"
          >反选</el-button
        >
        <el-button type="primary" size="small" @click="batchDeleteGoods"
          >批量删除</el-button
        >
      </div>
    </div>
    <!-- 分页菜单 -->
    <div class="pages">
      <MyPagination
        :total="total"
        :pageSize="pageSize"
        :currentPage.sync="currentPage"
        @changePages="changePages"
      />
      <AddDialog
        ref="dialog"
        @getGoodPage="getGoodPage"
        :title="title"
        :rowData="rowData"
      />
    </div>
  </div>
</template>

<script>
import AddDialog from "./AddDialog .vue";
import MyPagination from "../../components/MyPagination.vue";
export default {
  components: { MyPagination, AddDialog },
  data() {
    return {
      input: "",
      tableData: [],
      list: [],
      total: 10,
      pageSize: 1,
      type: true,
      currentPage: 1,
      title: "",
      rowData: {},
      goodsSelected: [],
    };
  },
  methods: {
    // 编辑商品
    handleEdit(index, row) {
      this.title = "商品编辑";
      console.log(index, row);
      this.$refs.dialog.dialogVisible = true;
      this.rowData = { ...row };
    },
    // 删除商品
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .deleteGoods({
              id: row.id,
            })
            .then((res) => {
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                // 更新视图
                this.getGoodPage(1);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取选中的行数据
    handleSelectionChange(val) {
      this.goodsSelected = val;
      console.log(this.goodsSelected);
    },
    //反选
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    //批量删除
    batchDeleteGoods() {
      var arr = [];
      this.goodsSelected.forEach((item) => {
        arr.push(item.id);
        // console.log(typeof arr);
      });
      console.log(arr);
      this.$api.batchDelete({ idArr: arr }).then((res) => {
        //sconsole.log(res.data);
        if (res.data.status === 200) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 更新视图
          this.getGoodPage(1);
        }
      });
    },
    // 全选
    selectAll() {
      this.$refs.multipleTable.toggleAllSelection();
    },

    //获取页码
    changePages(num) {
      this.currentPage = num;
      if (this.type == true) {
        this.getGoodPage(num); //商品分页的时候
      } else {
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    // 控制弹框显示
    addGoods() {
      // 通过ref属性获取真实dom元素，更改其内部属性
      this.$refs.dialog.dialogVisible = true;
      this.title = "商品添加";
    },
    //搜索操作
    searchIpu(val) {
      // 如果取消搜索、没有搜索时
      if (!val) {
        this.getGoodPage(1);
        this.currentPage = 1;
        this.type = true;
        return;
      }
      this.$api
        .getSearch({
          search: val,
        })
        .then((res) => {
          this.currentPage = 1;
          if (res.data.status === 200) {
            this.list = res.data.result;
            this.total = res.data.result.length;
            this.pageSize = 3;
            this.tableData = res.data.result.slice(0, 3);
            this.type = false;
          } else {
            this.tableData = [];
            this.total = 1;
            this.pageSize = 1;
            this.type = true;
          }
        });
    },
    // 获取商品页数据
    getGoodPage(page) {
      this.$api
        .getGoodList({
          page,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.tableData = res.data.data;
            this.total = res.data.total;
            this.pageSize = res.data.pageSize;
          }
        });
    },
  },
  created() {
    this.getGoodPage(1);
    this.title = "商品编辑";
  },
};
</script>

<style lang='less' scoped>
.goods {
  margin: 20px;
}
.header {
  display: flex;
  margin: 20px 0;
  button {
    margin-left: 20px;
  }
}
</style>